<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Hieu Ho
 */

class Timkiem_giasu_model extends CI_Model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $ci = & get_instance();
        $ci->load->helper('string');
    }

    public function index($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc, $number, $offset) {

        //----------------------


        return $this->TimKiem_LamGiaSu($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc, $number, $offset);







        //loc trinh do
        //$idTrinhDo='' 
        //loc tem mon hoc
        //locgioitinh
        //.....
    }

// 


    public function getTrinhDo() {
        $this->db->select("id,trinhdo");
        $td = $this->db->get("gs7s_trinhdo");
        $td = $td->result_array();
        return $td;
    }

    public function getNganhHoc() {
        $mh = $this->db->get("gs7s_nganhhoc");
        $mh = $mh->result_array();
        return $mh;
    }

    public function getMonHoc() {

        $this->db->get("gs7s_nganhhoc");
        $this->db->join("gs7s_nganhhoc", 'gs7s_monhoc.NganhHoc_id = gs7s_nganhhoc.id');
        $nh = $this->db->get('gs7s_monhoc');
        $nh = $nh->result_array();
        return $nh;
    }

    public function TimKiem_LamGiaSu($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc, $number, $offset) {
        //trả về toàn bộ dữ liệu trong bảng LamGiaSu vào mảng $data
        $this->db->select("*");

        $this->db->get("gs7s_lamgiasu");
        $this->db->where('LamGiaSu_TrangThai_id', 1);
        $this->db->join("gs7s_thanhvien", 'gs7s_lamgiasu.thanhvien_id = gs7s_thanhvien.id');

        if ($idTrinhDo != 0) {
            $this->db->where('TrinhDo_id', $idTrinhDo);
        }
        if ($TenMonHoc != "") {
            foreach ($TenMonHoc as $value) {
                $this->db->like('MonHoc', $value);
            }
        }

        if ($TenThanhVien != "") {
            $this->db->like('tenhienthi', $TenThanhVien);
        }
        if ($idGioiTinh != "") {
            $this->db->where('gs7s_thanhvien.gioitinh', $idGioiTinh);
        }

        if ($number == NULL && $offset == NULL) {
            return count($this->db->get("gs7s_lamgiasu")->result_array());
        }
        $this->db->limit($number, $offset);

        $this->db->select('gs7s_lamgiasu.id,thanhvien_id,TrinhDo_id,MonHoc,KhuVucDay,MucLuongYeuCau');
        $this->db->order_by("gs7s_lamgiasu.id","desc");
        $data = $this->db->get("gs7s_lamgiasu")->result_array();

        $i = 0;
        foreach ($data as $value) {
            //----------- load thong tin thanh vien------------------
            $this->db->where("id", $value['thanhvien_id']);
            $this->db->select("username,tenhienthi,gioitinh,email,sodienthoai,anhdaidien");
            $thanhvien = $this->db->get("gs7s_thanhvien");
            $rthanhvien = $thanhvien->result_array();
            $data[$i]['username'] = $rthanhvien[0]['username'];
            $data[$i]['tenhienthi'] = $rthanhvien[0]['tenhienthi'];
            $data[$i]['gioitinh'] = $rthanhvien[0]['gioitinh'];
            $data[$i]['email'] = $rthanhvien[0]['email'];
            $data[$i]['anhdaidien'] = $rthanhvien[0]['anhdaidien'];
            //-------------load trinh do --------------
            $this->db->where("id", $value['TrinhDo_id']);
            $this->db->select("TrinhDo");
            $trinhdo = $this->db->get("gs7s_trinhdo");
            $rtrinhdo = $trinhdo->result_array();
            $data[$i]['trinhdo'] = $rtrinhdo[0]['TrinhDo'];
            $i = $i + 1;
        }
        return $data;
    }

}

?>
